package com.jay.test.designModel.creater.singleton;

/**
 * 懒汉式
 * 问题: 每次调用getInstance都要同步(synchronized), 效率降低
 * Created by jifang on 15/12/4.
 */
public class LazySingleton {

    /**
     * 类加载时并没初始化, 延迟加载
     */
    private static LazySingleton instance;

    private LazySingleton() {
    }

    /**
     * 注意synchronized, 线程安全
     */
    public static synchronized LazySingleton getInstance() {
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }

    @Override
    public String toString() {
        return "LazySingleton{}";
    }
}